# -*- coding: utf-8 -*-
#!/usr/bin/env python3

import sys
import os
# import shutil

def process_file_by_title(fileobj, parpath, childname):
    filename = parpath + os.sep + childname
    if not os.path.isfile(filename):
        print("is not a file:"+filename)
        return 0

    # basefilename = os.path.splitext(currenfile)[0]
    with open(filename, 'r', encoding='utf-8', newline='\n') as f:
        count = 0
        while count < 4:
            oneline = f.readline()
            # print(oneline)
            count += 1

            hasalias = oneline[:5]
            if hasalias == "title":
                newbasename = oneline[7:-1]
                newbasename.lstrip()
                newbasename.rstrip()
                print(newbasename)

                newfilename = parpath + os.sep + newbasename + ".md"
                # 找到alias行
                newline = 'mv "' + filename + '"  "' + newfilename + '"'
                print(newline)
                fileobj.write(newline)
                fileobj.write("\n")
                break
    return 1
def process_file(fileobj, parpath, childname):
    filename = parpath + os.sep + childname
    if not os.path.isfile(filename):
        print("is not a file:"+filename)
        return 0

    # basefilename = os.path.splitext(currenfile)[0]
    with open(filename, 'r', encoding='utf-8', newline='\n') as f:
        count = 0
        while count < 4:
            oneline = f.readline()
            # print(oneline)
            count += 1

            hasalias = oneline[:5]
            if hasalias == "alias":
                newbasename = oneline[7:-1]
                newbasename.lstrip()
                newbasename.rstrip()
                print(newbasename)

                newfilename = parpath + os.sep + newbasename + ".md"
                # 找到alias行
                newline = 'mv "' + filename + '"  "' + newfilename + '"'
                print(newline)
                fileobj.write(newline)
                fileobj.write("\n")
                break
    return 1


def process_subdir(fileobj, parpath):
    for currenfile in os.listdir(parpath):
        # print("currentfile:"+currenfile)
        fullpath = parpath + os.sep + currenfile
        if os.path.isdir(fullpath):
            continue
        else:
            ext = os.path.splitext(currenfile)[1]
            # print("ext:"+ext)
            if ext not in [".md"]:
                continue
            process_file(fileobj,  parpath, currenfile)
    return 1

def process_dir(fileobj, parpath):
    for currenfile in os.listdir(parpath):
        # print("subcurrentfile:"+currenfile)
        subfullpath = parpath + os.sep + currenfile
        # print(subfullpath)
        if os.path.isdir(subfullpath):
            process_subdir(fileobj, subfullpath)
        # else:
        #     ext = os.path.splitext(currenfile)[1]
        #     print("ext:"+ext)
        #     if ext not in [".md"]:
        #         continue
        #     process_file(fileobj,  parpath, currenfile)

if __name__ == '__main__':
    filecwd = "/home/dasea/learn/obnote/200-SoftwareNote/obsidian"
    print(filecwd)

    # open file and 写入
    cache_path = "rename.sh"
    f = open(cache_path, 'wt', encoding='utf-8', newline='\n')

    print(os.listdir(filecwd))
    for currenfile in os.listdir(filecwd):
        print("currentfile:"+currenfile)
        if (currenfile == "800-Other"):
            print("Is other dir")
            continue
        elif (currenfile == "600-Obsidian"):
            print("Is template dir")
            continue
        elif (currenfile == "600-Obsidian/Attachment"):
            print("Is attachment dir")
        elif (currenfile == "README.md"):
            print("Is readme")
        elif (currenfile == ".obsidian"):
            print("Is obsidian")
        else:
            print("===================================")
            process_file_by_title(f, filecwd, currenfile)
            # fullpath = filecwd + os.sep + currenfile
            # print(fullpath)
            # if os.path.isdir(fullpath):
            #     process_dir(f, fullpath)

        # ext = os.path.splitext(currenfile)[1]
        # print(ext)
        # if (ext not in [".md"]):
        #     continue

        # print(newname)
        # shutil.move(currenfile, newname)
